package com.dianping.cat.consumer.cross.model.transform;
import com.dianping.cat.consumer.cross.model.IVisitor;
import com.dianping.cat.consumer.cross.model.entity.CrossReport;
import com.dianping.cat.consumer.cross.model.entity.Local;
import com.dianping.cat.consumer.cross.model.entity.Name;
import com.dianping.cat.consumer.cross.model.entity.Remote;
import com.dianping.cat.consumer.cross.model.entity.Type;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;

public class DefaultNativeParser implements IVisitor {
  private DefaultLinker m_linker;

  public DefaultNativeParser(InputStream in) {
/*  17 */     this.m_linker = new DefaultLinker(true);




/*  22 */     this.m_in = new DataInputStream(in);
  }
  private DataInputStream m_in;

/*  26 */   public static CrossReport parse(byte[] data) { return parse(new ByteArrayInputStream(data)); }


  public static CrossReport parse(InputStream in) {
/*  30 */     DefaultNativeParser parser = new DefaultNativeParser(in);
    CrossReport crossReport = new CrossReport();

    try {
/*  34 */       crossReport.accept(parser);
/*  35 */     } catch (RuntimeException e) {
      if (e.getCause() == null || !(e.getCause() instanceof java.io.EOFException))
      {

/*  39 */         throw e;
      }
    }

/*  43 */     parser.m_linker.finish();
/*  44 */     return crossReport;
  }



  public void visitCrossReport(CrossReport crossReport) {
    byte tag;
/*  51 */     if ((tag = readTag()) != -4) {
/*  52 */       throw new RuntimeException(String.format("Malformed payload, expected: %s, but was: %s!", new Object[] { Integer.valueOf(-4), Byte.valueOf(tag) }));
    }

/*  55 */     while ((tag = readTag()) != -1) {
/*  56 */       visitCrossReportChildren(crossReport, (tag & 0xFF) >> 2, tag & 0x3);
    }
  }

  protected void visitCrossReportChildren(CrossReport crossReport, int _field, int _type) {
/*  61 */     switch (_field) {
      case 1:
/*  63 */         crossReport.setDomain(readString());
        break;
      case 2:
/*  66 */         crossReport.setStartTime(readDate());
        break;
      case 3:
/*  69 */         crossReport.setEndTime(readDate());
        break;
      case 4:
/*  72 */         if (_type == 1) {
/*  73 */           crossReport.addDomain(readString()); break;
/*  74 */         }  if (_type == 2) {
/*  75 */           for (int i = readInt(); i > 0; i--) {
/*  76 */             crossReport.addDomain(readString());
          }
        }
        break;
      case 5:
/*  81 */         if (_type == 1) {
/*  82 */           crossReport.addIp(readString()); break;
/*  83 */         }  if (_type == 2) {
/*  84 */           for (int i = readInt(); i > 0; i--) {
/*  85 */             crossReport.addIp(readString());
          }
        }
        break;
      case 33:
/*  90 */         if (_type == 1) {
/*  91 */           Local local = new Local();

/*  93 */           visitLocal(local);
/*  94 */           this.m_linker.onLocal(crossReport, local); break;
/*  95 */         }  if (_type == 2) {
/*  96 */           for (int i = readInt(); i > 0; i--) {
/*  97 */             Local local = new Local();

/*  99 */             visitLocal(local);
/* 100 */             this.m_linker.onLocal(crossReport, local);
          }
        }
        break;
    }
  }



  public void visitLocal(Local local) {
    byte tag;
/* 111 */     while ((tag = readTag()) != -1) {
/* 112 */       visitLocalChildren(local, (tag & 0xFF) >> 2, tag & 0x3);
    }
  }

  protected void visitLocalChildren(Local local, int _field, int _type) {
/* 117 */     switch (_field) {
      case 1:
/* 119 */         local.setId(readString());
        break;
      case 33:
/* 122 */         if (_type == 1) {
/* 123 */           Remote remote = new Remote();

/* 125 */           visitRemote(remote);
/* 126 */           this.m_linker.onRemote(local, remote); break;
/* 127 */         }  if (_type == 2) {
/* 128 */           for (int i = readInt(); i > 0; i--) {
/* 129 */             Remote remote = new Remote();

/* 131 */             visitRemote(remote);
/* 132 */             this.m_linker.onRemote(local, remote);
          }
        }
        break;
    }
  }



  public void visitName(Name name) {
    byte tag;
/* 143 */     while ((tag = readTag()) != -1) {
/* 144 */       visitNameChildren(name, (tag & 0xFF) >> 2, tag & 0x3);
    }
  }

  protected void visitNameChildren(Name name, int _field, int _type) {
/* 149 */     switch (_field) {
      case 1:
/* 151 */         name.setId(readString());
        break;
      case 2:
/* 154 */         name.setTotalCount(readLong());
        break;
      case 3:
/* 157 */         name.setFailCount(readInt());
        break;
      case 4:
/* 160 */         name.setFailPercent(readDouble());
        break;
      case 5:
/* 163 */         name.setAvg(readDouble());
        break;
      case 6:
/* 166 */         name.setSum(readDouble());
        break;
      case 7:
/* 169 */         name.setTps(readDouble());
        break;
    }
  }



  public void visitRemote(Remote remote) {
    byte tag;
/* 178 */     while ((tag = readTag()) != -1)
/* 179 */       visitRemoteChildren(remote, (tag & 0xFF) >> 2, tag & 0x3); 
  }

  protected void visitRemoteChildren(Remote remote, int _field, int _type) {
    Type type;
/* 184 */     switch (_field) {
      case 1:
/* 186 */         remote.setId(readString());
        break;
      case 2:
/* 189 */         remote.setRole(readString());
        break;
      case 3:
/* 192 */         remote.setApp(readString());
        break;
      case 4:
/* 195 */         remote.setIp(readString());
        break;
      case 33:
/* 198 */         type = new Type();

/* 200 */         visitType(type);
/* 201 */         this.m_linker.onType(remote, type);
        break;
    }
  }



  public void visitType(Type type) {
    byte tag;
/* 210 */     while ((tag = readTag()) != -1) {
/* 211 */       visitTypeChildren(type, (tag & 0xFF) >> 2, tag & 0x3);
    }
  }

  protected void visitTypeChildren(Type type, int _field, int _type) {
/* 216 */     switch (_field) {
      case 1:
/* 218 */         type.setId(readString());
        break;
      case 2:
/* 221 */         type.setTotalCount(readLong());
        break;
      case 3:
/* 224 */         type.setFailCount(readInt());
        break;
      case 4:
/* 227 */         type.setFailPercent(readDouble());
        break;
      case 5:
/* 230 */         type.setAvg(readDouble());
        break;
      case 6:
/* 233 */         type.setSum(readDouble());
        break;
      case 7:
/* 236 */         type.setTps(readDouble());
        break;
      case 33:
/* 239 */         if (_type == 1) {
/* 240 */           Name name = new Name();

/* 242 */           visitName(name);
/* 243 */           this.m_linker.onName(type, name); break;
/* 244 */         }  if (_type == 2) {
/* 245 */           for (int i = readInt(); i > 0; i--) {
/* 246 */             Name name = new Name();

/* 248 */             visitName(name);
/* 249 */             this.m_linker.onName(type, name);
          }
        }
        break;
    }
  }

  private Date readDate() {
    try {
/* 258 */       return new Date(readVarint(64));
/* 259 */     } catch (IOException e) {
/* 260 */       throw new RuntimeException(e);
    }
  }

  private double readDouble() {
    try {
/* 266 */       return Double.longBitsToDouble(readVarint(64));
/* 267 */     } catch (IOException e) {
/* 268 */       throw new RuntimeException(e);
    }
  }

  private int readInt() {
    try {
/* 274 */       return (int)readVarint(32);
/* 275 */     } catch (IOException e) {
/* 276 */       throw new RuntimeException(e);
    }
  }

  private long readLong() {
    try {
/* 282 */       return readVarint(64);
/* 283 */     } catch (IOException e) {
/* 284 */       throw new RuntimeException(e);
    }
  }

  private String readString() {
    try {
/* 290 */       return this.m_in.readUTF();
/* 291 */     } catch (IOException e) {
/* 292 */       throw new RuntimeException(e);
    }
  }

  private byte readTag() {
    try {
/* 298 */       return this.m_in.readByte();
/* 299 */     } catch (IOException e) {
/* 300 */       throw new RuntimeException(e);
    }
  }

  protected long readVarint(int length) throws IOException {
/* 305 */     int shift = 0;
/* 306 */     long result = 0L;

/* 308 */     while (shift < length) {
/* 309 */       byte b = this.m_in.readByte();
/* 310 */       result |= (b & 0x7F) << shift;
/* 311 */       if ((b & 0x80) == 0) {
/* 312 */         return result;
      }
/* 314 */       shift += 7;
    }

/* 317 */     throw new RuntimeException("Malformed variable int " + length + "!");
  }
}


/* Location:              /Users/hanfei/Documents/apache-tomcat-9.0.24/webapps/cat/WEB-INF/lib/cat-consumer-3.0.0.jar!/com/dianping/cat/consumer/cross/model/transform/DefaultNativeParser.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       1.0.7
 */